package com.xudow.app.ui;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.activeshare.edu.ucenter.models.base.AgencySchools;
import com.activeshare.edu.ucenter.models.course.CourseSearchCriteriaLevelOneWithOthers;
import com.activeshare.edu.ucenter.models.searchcriteria.CourseSearchCriteriaLevelThreeWithOthers;
import com.activeshare.edu.ucenter.models.searchcriteria.CourseSearchCriteriaLevelTwoWithOthers;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.data.CourseData;
import com.xudow.app.data.Pair;
import com.xudow.app.ui.adapter.CourseFilterMenuAdapter;
import com.xudow.app.ui.task.AgencyschoolsTask;
import com.xudow.app.util.Lists;

import java.util.ArrayList;
import java.util.List;

/**
 * 课程搜索Activity
 *
 * @author lukecheng
 */
public class CourseSearchActivity extends BaseActivity
        implements
        CourseLocationPopupWindow.CourseLocationListener {

    private LayoutInflater inflater;
    private List<AgencySchools> agencyshools;//大帝学校列表
    private PopupWindow artSportMenu;
    private PopupWindow cultureMenu;

    private String sort;

    private TextView sortTextView;

    //艺体类view
//    private TextView artSportTitle;
//    private ImageView artSportIcon;

    //文化课view
    private TextView cultureTitle;
    private ImageView cultureIcon;

    private EditText keywordEditText;
    private RelativeLayout rootLayout;
    private LinearLayout headerBarLayout;

    /**
     * 筛选弹出
     */
    private CourseFilterPopupWindow filterPopupWindow;
    private CourseLocationPopupWindow courseLocationPopupWindow,courseLocationPopupWindow2 ;

    private String currentAreaName; //当前区域名
    private String currentAreaValue; //当前区域值
    private String currentAgencySchoolId;//当前大帝区域学校ID

    /**
     * 排序
     */
    public static final int SORT_OPTION_DEFAULT = 0; //默认
    public static final int SORT_OPTION_LATEST = 1;  //开课时间最近
    public static final int SORT_OPTION_NEARBY = 2;  //地点离我最近

    private int currentSortOption = SORT_OPTION_DEFAULT;

    /**
     * 3级选项
     */
    private List<Pair> menuOptions0, menuOptions1, menuOptions2;
    private int lastselected_one=-1;
    private int lastselected_two=-1;
    private int lastselected_three=-1;
    private CourseListFragment courseListFragment;

    //当前筛选项
    private static final int CURRENT_FILTER_ART_APORT = 1; //艺体类
    private static final int CURRENT_FILTER_CULTRAL = 2;  //文化类
    private int currentFilter;

    //筛选条件
    private String levelOneSelected;
    private String levelTwoSelected;
    private String levelThreeSelected;
    StringBuffer buffer2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_course_search);

        inflater = getLayoutInflater();

        currentAreaName = getString(R.string.no_limit);

        rootLayout = (RelativeLayout) findViewById(R.id.root);
        headerBarLayout = (LinearLayout) findViewById(R.id.header_bar);
        keywordEditText = (EditText) findViewById(R.id.keyword);

        sortTextView = (TextView) findViewById(R.id.sort);
        //      artSportTitle = (TextView) findViewById(R.id.art_sport_title);
//        artSportIcon = (ImageView) findViewById(R.id.art_sport_icon);

        cultureTitle = (TextView) findViewById(R.id.culture_title);
        cultureIcon = (ImageView) findViewById(R.id.culture_icon);

        courseListFragment = (CourseListFragment)
                getSupportFragmentManager().findFragmentById(R.id.course_fragment);
         buffer2=new StringBuffer();

        SharedPreferences preference2 = this.getSharedPreferences("nianjiid", Context.MODE_PRIVATE);

        int datasize2 = preference2.getInt("datasize", 0);

        for(int i=0;i<datasize2;i++){
            int datagetId2 = preference2.getInt("datagetId"+i, 0);

            buffer2.append(datagetId2+",");
        }
        if( buffer2.length()!=0){
            buffer2.deleteCharAt(buffer2.length()-1);
        }

        courseListFragment.loadCourses(null,null,null,null,null,null,null,null,null,null,null,null,null,null);
    }

    public void onBackBtnClick(View view) {
        finish();
    }

    /**
     * 搜索按钮点击
     *
     * @param view
     */
    public void onSearchClick(View view) {
        InputMethodManager imm = (InputMethodManager)
                view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);

        levelOneSelected = null;
        levelTwoSelected = null;
        levelThreeSelected = null;

        clearOptions();

        startSearch(false);
    }

    /**
     * 排序点击事件
     *
     * @param view
     */
    public void onArtSportMenuClick(View view) {
        if (null == artSportMenu || !artSportMenu.isShowing()) {
            //设置筛选标题颜色
//            artSportTitle.setTextColor(getResources().getColor(R.color.rvcode));
//            artSportIcon.setImageResource(R.mipmap.ico_green_top_arrow);

            //选项三级数据
            menuOptions0 = CourseData.artSportCriterialLevelOne();
            menuOptions1 = Lists.newArrayList();
            menuOptions2 = Lists.newArrayList();

            if (CURRENT_FILTER_ART_APORT == currentFilter) { //  标识上次选中的item
                //展开选项
                for (Pair pair : menuOptions0) {
                    if (pair.second == levelOneSelected) {
                        pair.checked = true;

                        menuOptions1.addAll(CourseData.artSportCriterialLevelTwo(pair.second));

                        for (Pair level2Pair : menuOptions1) {
                            if (level2Pair.second == levelTwoSelected) {
                                List<Pair> selectedLevelThreeData = CourseData.artSportCriterialLevelThree(level2Pair.second);

                                if (selectedLevelThreeData.size() > 0) {
                                    //有下一级
                                    level2Pair.checked = true;
                                } else {
                                    //只有两级数据
                                    level2Pair.selected = true;
                                }

                                if (null != levelThreeSelected) {
                                    menuOptions2.addAll(selectedLevelThreeData);

                                    for (Pair level3Pair : menuOptions2) {
                                        if (level3Pair.second == levelThreeSelected) {
                                            level3Pair.selected = true;
                                            break;
                                        }
                                    }
                                }

                                break;
                            }
                        }

                        break;
                    }
                }
            }

            View courseFilterMenuView = this.getLayoutInflater().inflate(R.layout.layout_course_filter_menu, null, false);
            artSportMenu = new PopupWindow(courseFilterMenuView, LinearLayout.LayoutParams.MATCH_PARENT,
                    708);
            artSportMenu.setOutsideTouchable(true);
            artSportMenu.setFocusable(true);
            artSportMenu.setBackgroundDrawable(new BitmapDrawable());

            final CourseFilterMenuAdapter artSportOption0Adapter = new CourseFilterMenuAdapter(this,
                    R.layout.item_course_filter_menu, menuOptions0);
            final CourseFilterMenuAdapter artSportOption1Adapter = new CourseFilterMenuAdapter(this,
                    R.layout.item_course_filter_menu_grey, menuOptions1);
            final CourseFilterMenuAdapter artSportOption2Adapter = new CourseFilterMenuAdapter(this,
                    R.layout.item_course_filter_menu_grey, menuOptions2);

            final ListView artSportOption0ListView = (ListView) courseFilterMenuView.findViewById(R.id.data_list0);
            final ListView artSportOption1ListView = (ListView) courseFilterMenuView.findViewById(R.id.data_list1);
            final ListView artSportOption2ListView = (ListView) courseFilterMenuView.findViewById(R.id.data_list2);

            artSportOption0ListView.setAdapter(artSportOption0Adapter);
            artSportOption1ListView.setAdapter(artSportOption1Adapter);
            artSportOption2ListView.setAdapter(artSportOption2Adapter);

            //第一级ListView点击
            artSportOption0ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    for (int i = 0; i < menuOptions0.size(); i++) {
                        menuOptions0.get(i).checked = false;
                    }

                    levelOneSelected = menuOptions0.get(position).second;
                    levelTwoSelected = "";
                    levelThreeSelected = "";

                    menuOptions0.get(position).checked = true;
                    artSportOption0Adapter.notifyDataSetChanged();

                    //第二级
                    menuOptions1.clear();

                    List<Pair> level2OptionsTmp = CourseData.artSportCriterialLevelTwo(menuOptions0.get(position).second);
                    if (level2OptionsTmp.size() <= 0) {  // 如果二级筛选为空 直接搜索
                        clearOptions();
//                        artSportTitle.setText(menuOptions0.get(position).first);

                        //当前筛选项
                        currentFilter = CURRENT_FILTER_ART_APORT;

                        startSearch(false);
                        artSportMenu.dismiss();
                    }
                    menuOptions1.addAll(level2OptionsTmp);

                    menuOptions2.clear();
                    artSportOption2Adapter.notifyDataSetChanged();

                    artSportOption1Adapter.notifyDataSetChanged();
                    artSportOption1ListView.setVisibility(View.VISIBLE);
                }
            });

            //第二级ListView点击
            artSportOption1ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    for (int i = 0; i < menuOptions1.size(); i++) {
                        menuOptions1.get(i).checked = false;
                    }

                    levelTwoSelected = menuOptions1.get(position).second;
                    levelThreeSelected = "";

                    List<Pair> level3Data = CourseData.artSportCriterialLevelThree(menuOptions1.get(position).second);

                    if (level3Data.size() > 0) {
                        menuOptions1.get(position).checked = true;
                    } else {
                        clearOptions();
//                        artSportTitle.setText(menuOptions1.get(position).first);

                        //当前筛选项
                        currentFilter = CURRENT_FILTER_ART_APORT;

                        startSearch(false);
                        artSportMenu.dismiss();
                    }

                    artSportOption1Adapter.notifyDataSetChanged();

                    menuOptions2.clear();
                    menuOptions2.addAll(level3Data);

                    artSportOption2Adapter.notifyDataSetChanged();
                    artSportOption1ListView.setVisibility(View.VISIBLE);
                }
            });

            //第三级ListView点击
            artSportOption2ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    levelThreeSelected = menuOptions2.get(position).second;

                    clearOptions();
//                    artSportTitle.setText(menuOptions2.get(position).first);

                    //当前筛选项
                    currentFilter = CURRENT_FILTER_ART_APORT;

                    startSearch(false);

                    artSportMenu.dismiss();
                }
            });

            artSportMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
//                    artSportTitle.setTextColor(getResources().getColor(android.R.color.secondary_text_light));
//                    artSportIcon.setImageResource(R.mipmap.ic_course_search_arrow_down);
                }
            });

            artSportMenu.showAsDropDown(view, 0, 0);
        } else {
//            artSportTitle.setTextColor(getResources().getColor(android.R.color.secondary_text_light));
//            artSportIcon.setImageResource(R.mipmap.ic_course_search_arrow_down);

            artSportMenu.dismiss();
        }
    }

    /**
     * 清空选项
     */
    private void clearOptions() {
//        artSportTitle.setText(getString(R.string.art_sport_category));
        cultureTitle.setText(getString(R.string.culture_category));
    }

    /**
     * 文化课点击
     *
     * @param view
     */
    public void onCultureMenuClick(View view) {
        if (null == cultureMenu || !cultureMenu.isShowing()) {

            //设置筛选标题颜色
            cultureTitle.setTextColor(getResources().getColor(R.color.rvcode));
            cultureIcon.setImageResource(R.mipmap.ico_green_top_arrow);

            //选项三级数据
            menuOptions0 = CourseData.cultureCriterialLevelOne();
            menuOptions1 = Lists.newArrayList();
            menuOptions2 = Lists.newArrayList();

            if (CURRENT_FILTER_CULTRAL == currentFilter) {
                //展开选项
                for (Pair pair : menuOptions0) {
                    if (pair.second == levelOneSelected) {
                        pair.checked = true;

                        menuOptions1.addAll(CourseData.cultureCriterialLevelTwo(pair.second));

                        for (Pair level2Pair : menuOptions1) {
                            if (level2Pair.second == levelTwoSelected) {
                                List<Pair> selectedLevelThreeData = CourseData.cultureCriterialLevelThree(level2Pair.second);

                                if (selectedLevelThreeData.size() > 0) {
                                    //有下一级
                                    level2Pair.checked = true;
                                } else {
                                    //只有两级数据
                                    level2Pair.selected = true;
                                }

                                if (null != levelThreeSelected) {
                                    menuOptions2.addAll(selectedLevelThreeData);

                                    for (Pair level3Pair : menuOptions2) {
                                        if (level3Pair.second == levelThreeSelected) {
                                            level3Pair.selected = true;
                                            break;
                                        }
                                    }
                                }

                                break;
                            }
                        }

                        break;
                    }
                }
            }

            View courseFilterMenuView = getLayoutInflater().inflate(R.layout.layout_course_filter_menu, null, false);
            cultureMenu = new PopupWindow(courseFilterMenuView, LinearLayout.LayoutParams.MATCH_PARENT,
                    708);
            cultureMenu.setOutsideTouchable(true);
            cultureMenu.setFocusable(true);
            cultureMenu.setBackgroundDrawable(new BitmapDrawable());

            final CourseFilterMenuAdapter cultureOption0Adapter = new CourseFilterMenuAdapter(this,
                    R.layout.item_course_filter_menu, menuOptions0);
            final CourseFilterMenuAdapter cultureOption1Adapter = new CourseFilterMenuAdapter(CourseSearchActivity.this,
                    R.layout.item_course_filter_menu_grey, menuOptions1);
            final CourseFilterMenuAdapter cultureOption2Adapter = new CourseFilterMenuAdapter(CourseSearchActivity.this,
                    R.layout.item_course_filter_menu_grey, menuOptions2);

            final ListView cultureOption0ListView = (ListView) courseFilterMenuView.findViewById(R.id.data_list0);
            final ListView cultureOption1ListView = (ListView) courseFilterMenuView.findViewById(R.id.data_list1);
            final ListView cultureOption2ListView = (ListView) courseFilterMenuView.findViewById(R.id.data_list2);

            cultureOption0ListView.setAdapter(cultureOption0Adapter);
            cultureOption1ListView.setAdapter(cultureOption1Adapter);
            cultureOption2ListView.setAdapter(cultureOption2Adapter);

            //第一级ListView点击
            cultureOption0ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    for (int i = 0; i < menuOptions0.size(); i++) {
                        menuOptions0.get(i).checked = false;
                    }

                    levelOneSelected = menuOptions0.get(position).second;
                    levelTwoSelected = "";
                    levelThreeSelected = "";

                    menuOptions0.get(position).checked = true;
                    cultureOption0Adapter.notifyDataSetChanged();

                    //第二级
                    menuOptions1.clear();

                    List<Pair> level2OptionsTmp = CourseData.cultureCriterialLevelTwo(menuOptions0.get(position).second);
                    if (level2OptionsTmp.size() <= 0) {
                        clearOptions();
                        cultureTitle.setText(menuOptions0.get(position).first);

                        //当前筛选项
                        currentFilter = CURRENT_FILTER_CULTRAL;

                        startSearch(false);

                        cultureMenu.dismiss();
                    }
                    menuOptions1.addAll(level2OptionsTmp);

                    menuOptions2.clear();
                    cultureOption2Adapter.notifyDataSetChanged();

                    cultureOption1Adapter.notifyDataSetChanged();
                    cultureOption1ListView.setVisibility(View.VISIBLE);
                }
            });

            //第二级ListView点击
            cultureOption1ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    for (int i = 0; i < menuOptions1.size(); i++) {
                        menuOptions1.get(i).checked = false;
                    }

                    levelTwoSelected = menuOptions1.get(position).second;
                    levelThreeSelected = "";

                    List<Pair> level3Data = CourseData.cultureCriterialLevelThree(menuOptions1.get(position).second);

                    if (level3Data.size() > 0) {
                        menuOptions1.get(position).checked = true;
                    } else {
                        clearOptions();
                        cultureTitle.setText(menuOptions1.get(position).first);

                        //当前筛选项
                        currentFilter = CURRENT_FILTER_CULTRAL;

                        startSearch(false);

                        cultureMenu.dismiss();
                    }

                    cultureOption1Adapter.notifyDataSetChanged();

                    menuOptions2.clear();
                    menuOptions2.addAll(level3Data);

                    cultureOption2Adapter.notifyDataSetChanged();
                    cultureOption1ListView.setVisibility(View.VISIBLE);
                }
            });

            //第三级ListView点击
            cultureOption2ListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    levelThreeSelected = menuOptions2.get(position).second;

                    clearOptions();
                    cultureTitle.setText(menuOptions2.get(position).first);

                    //当前筛选项
                    currentFilter = CURRENT_FILTER_CULTRAL;

                    startSearch(false);

                    cultureMenu.dismiss();
                }
            });

            cultureMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    cultureTitle.setTextColor(getResources().getColor(android.R.color.secondary_text_light));
                    cultureIcon.setImageResource(R.mipmap.ic_course_search_arrow_down);
                }
            });

            cultureMenu.showAsDropDown(view, 0, 0);
        } else {
            cultureTitle.setTextColor(getResources().getColor(android.R.color.secondary_text_light));
            cultureIcon.setImageResource(R.mipmap.ic_course_search_arrow_down);

            cultureMenu.dismiss();
        }
    }

    /**
     * 筛选按钮点击
     *
     * @param view
     */
    public void onFilterClick(View view) {
        if (null == filterPopupWindow || !filterPopupWindow.isShowing()) {
            WindowManager.LayoutParams lp = getWindow().getAttributes(); // 设置窗体透明度
            lp.alpha = 0.3f;
            getWindow().setAttributes(lp);

            //filterPopupWindow = new CourseFilterPopupWindow(this,this);
            //filterPopupWindow.setAreaName(currentAreaName);
            //filterPopupWindow.updateSortOption(currentSortOption);
            filterPopupWindow.showAtLocation(headerBarLayout, Gravity.RIGHT, 0, 0);
            filterPopupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    WindowManager.LayoutParams lp = getWindow().getAttributes(); // 恢复窗体透明度
                    lp.alpha = 1.0f;
                    getWindow().setAttributes(lp);
                }
            });
        }
    }

    /**
     * 地区选择按钮点击事件
     */
    /*@Override
    public void onLocationClick() {
        if (null == courseLocationPopupWindow || !courseLocationPopupWindow.isShowing()) {
            courseLocationPopupWindow = new CourseLocationPopupWindow(this, currentAreaValue,-1,this,null);
            courseLocationPopupWindow.showAtLocation(headerBarLayout, Gravity.RIGHT, 0, 0);
        }
    }*/
    //TODO 大帝校区选择点击事件
    public void ddOnLocationClick(){

        if (null == agencyshools) {
            loadingAgencySchoolList();
            showLodingDialog("正在加载校区");
        }
        else if (null == courseLocationPopupWindow2 || !courseLocationPopupWindow2.isShowing()) {

            courseLocationPopupWindow2 = new CourseLocationPopupWindow(CourseSearchActivity.this, currentAreaValue, 1,this,agencyshools);
            courseLocationPopupWindow2.showAtLocation(headerBarLayout, Gravity.RIGHT, 0, 0);
        }
    }
    private void loadingAgencySchoolList() {
        AgencyschoolsTask getschoollisttask=new AgencyschoolsTask(this,getSchoolListHandler);
        getschoollisttask.execute();
    }
    private Handler getSchoolListHandler=new Handler() {
        @Override
        public void handleMessage(Message msg) {
            hideLoadingDialog();
            if (msg.what==AgencyschoolsTask.SUCCESS){
                agencyshools=(List<AgencySchools>) msg.obj;

                courseLocationPopupWindow2 = new CourseLocationPopupWindow(CourseSearchActivity.this, currentAreaValue, 1,CourseSearchActivity.this,agencyshools);
                courseLocationPopupWindow2.showAtLocation(headerBarLayout, Gravity.RIGHT, 0, 0);
            }else
                getMyApp().makeToast("网络加载失败");
        }
    };
    /**
     * 筛选排序列表点击事件
     *
     * @param position
     */
   /* @Override
    public void onSortOptionClick(int position) {
        if (SORT_OPTION_NEARBY == position) {
            XApplication application = (XApplication) getApplication();
            if (null == application.longitude || null == application.latitude) {
                Toast.makeText(CourseSearchActivity.this,
                        getString(R.string.locate_fail),
                        Toast.LENGTH_SHORT).show();
            } else {
                currentSortOption = position;
            }
        } else {
            currentSortOption = position;
        }

        currentAreaName = null;
        currentAreaValue = null;
        currentAgencySchoolId=null;
        if (null != filterPopupWindow) {
           // filterPopupWindow.setAreaName(currentAreaName);
        }
    }

    *//**
     * 筛选PopupWindow确定按钮点击事件
     *//*
    @Override
    public void onSearchClick() {
        //传递参数
        levelOneSelected = null;
        levelTwoSelected = null;
        levelThreeSelected = null;

        clearOptions();

        startSearch(true);
    }*/

    /**
     * 区域列表选择事件
     *
     * @param pair
     */
    @Override
    public void onLocationSelected(Pair pair) {
        //清除排序
        currentSortOption = SORT_OPTION_DEFAULT;

        currentAreaName = pair.first;
        currentAreaValue = pair.second;

        //filterPopupWindow.setAreaName(currentAreaName);

        if (null != filterPopupWindow) {
            //filterPopupWindow.setSortOption(SORT_OPTION_DEFAULT);
        }
    }

    /**
     * 大帝学校区域列表选择事件
     * @param as
     */
    @Override
    public void onLocationSelected(AgencySchools as) {
        currentSortOption = SORT_OPTION_DEFAULT;

        currentAreaName = as.getName();
        currentAgencySchoolId = as.getId()+"";

        //filterPopupWindow.setDDAreaName(currentAreaName);

        if (null != filterPopupWindow) {
            //filterPopupWindow.setSortOption(SORT_OPTION_DEFAULT);
        }
    }

    /**
     * 搜索
     *
     * @param isPopupClick 是不是弹出窗口的确定按钮点击
     */
    private void startSearch(boolean isPopupClick) {
        if (!isPopupClick) {
            //恢复弹出窗口选项
            currentAreaValue = null;
            currentAreaName = null;
            currentAgencySchoolId=null;
            currentSortOption = SORT_OPTION_DEFAULT;
        }

        courseListFragment.setLevelOneSelected(levelOneSelected);

        if ("0".equals(levelTwoSelected)) {
            courseListFragment.setLevelTwoSelected(null);
        } else {
            courseListFragment.setLevelTwoSelected(levelTwoSelected);
        }

        if ("0".equals(levelThreeSelected)) {
            courseListFragment.setLevelThreeSelected(null);
        } else {
            courseListFragment.setLevelThreeSelected(levelThreeSelected);
        }

        String keyword = this.keywordEditText.getText().toString().trim();
        if (!TextUtils.isEmpty(keyword)) {
            courseListFragment.setKeyword(keyword);
        }

        courseListFragment.setAreaId(currentAreaValue);
        courseListFragment.setAgencySchoolId(currentAgencySchoolId);
        courseListFragment.setSortOption(currentSortOption);

        courseListFragment.loadCourses(null,null,null,null,null,null,null,null,null,null,null,null,null,null);

    }

}
